<?php
/**
 * Twitter Required Validator
 * Exntend CRequiredValidator to display custom message
 * @author luckymancvp
 */
class TRequiredValidator extends CRequiredValidator
{
    /**
     * Validates the attribute of the object.
     * If there is any error, the error message is added to the object.
     * @param CModel $object the object being validated
     * @param string $attribute the attribute being validated
     */
    protected function validateAttribute($object,$attribute)
    {
        $value=$object->$attribute; 
        // Blank value
        if($this->isEmpty($value,true))  {

            $this->addError(
                $object, 
                $attribute,
                Html::tag(
                    'p', 
                    array(
                        'class'=>'error active',
                        'role' =>'alert',
                    ),
                    $object->getAttributeLabel("$attribute")." is required!"
                )
            );
            return;
        }
    }

    /**
     * Returns the JavaScript needed for performing client-side validation.
     * @param CModel $object the data object being validated
     * @param string $attribute the name of the attribute to be validated.
     * @return string the client-side validation script.
     * @see CActiveForm::enableClientValidation
     */
    public function clientValidateAttribute($object,$attribute)
    {
     
        $error   = "";
        $success = "";
        switch ($attribute) {
            case 'full_name':
                $error   = "Full name is required!";
                $success = "Name looks great";
                break;
            case 'email':
                $error   = "What’s your email address?";
                $success = "We will email you a confirmation.";
                break;
            case 'password':
                $error   = "Password cannot be blank!";
                $success = "Password is perfect!";
                break;
            case 'username':
                $error   = "A username is required!";
                $success = "You can change it later.";
                break;
            
            default:
                # code...
                break;
        }
        $errorHtml = Html::tag(
            'p', 
            array(
                'class'=>'error active',
                'role' =>'alert',
            ),
            $error
        );
        $successHtml = Html::tag(
            'p', 
            array(
                'class'=>'isaok active',
                'role' =>'alert',
            ),
            $success
        );
        return " 
            if($.trim(value)=='') { 
            messages.push(".CJSON::encode($errorHtml)."); 
            } 
            else{ 
            messages.push(".CJSON::encode($successHtml)."); 
            } 
        "; 
    }
}
?>